1041C - Coffee Break - CodeForces Solution


binary search data structures greedy two pointers *1600

Please click on ads to support us..

Python Code:

import heapq
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def binary_search(c1, c2):
    m = (c1 + c2 + 1) // 2
    while abs(c1 - c2) > 1:
        m = (c1 + c2 + 1) // 2
        if ok(m):
            c2 = m
        else:
            c1 = m
    m = max(1, m - 1)
    while not ok(m):
        m += 1
    return m

def ok(m):
    for i in range(n - m):
        if b[i + m] - b[i] <= d:
            return False
    return True

n, m, d = map(int, input().split())
a = list(map(int, input().split()))
b = list(a)
b.sort()
l = binary_search(0, n)
h = []
for i in range(n):
    heapq.heappush(h, (a[i], i))
ans = [0] * n
now = 0
while h:
    _, i = heapq.heappop(h)
    ans[i] = now + 1
    now += 1
    now %= l
print(l)
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,l,x;
    cin>>n>>l>>x;
    int a[n];
    set<int>s;
    vector<pair<int,int>>v;
    for(int i=0;i<n;i++){
        cin>>a[i];
        s.insert(a[i]);
    }
    map<int,int>mp;
    int day=1;
    int filled=0;
    auto it = s.begin();
    while(filled<n){
        int k = *(it);
        mp[k] = day;
        filled++;
        s.erase(k);
        if(filled==n)break;
        it = s.lower_bound(k+x+1);
        //cout<<*(it)<<" ";
        if(it==s.end()){
            it = s.begin();
            day++;
        }
    }
    cout<<day<<endl;
    for(auto i:a){
        cout<<mp[i]<<" ";
    }
    cout<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment